// blanceWithdrawal/pages/blanceWithdrawal/blanceWithdrawal.js
var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        array: [],
        index: "",
        cardName: "",
        blankCard: "",
        money: "",
        page: 1,
        list: [],
        flag: true,
        features: "",
        m_apply: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            money: options.blanceNum
        });
        this.getBase();
        this.getRecordList();
        this.getBlanceSet();
    },
    writeCardName(e) {
        let val = e.detail.value;
        this.setData({
            cardName: val
        });
    },
    writeBlankCard(e) {
        let val = e.detail.value;
        //不是删除键按下面的来
                if (e.detail.keyCode != 8) {
            //去除空格
            val = e.detail.value.replace(/\s*/g, "");
            //只能输入数字和字母
                        if (/^[0-9a-zA-Z]+$/g.test(val)) {
                //4个字符加一个空格
                val = val.replace(/\s/g, "").replace(/(.{4})/g, "$1 ");
            } else {
                //输入其他字符则删除
                val = val.substr(0, val.length - 1);
                val = val.replace(/\s/g, "").replace(/(.{4})/g, "$1 ");
            }
        }
        this.setData({
            blankCard: val
        });
    },
    writeMoney(e) {
        let money = e.detail.value;
        // if(!(/^[0-9]+/g).test(money)){
        //   money = money.substr(0,money.length-1);
        // }
                this.setData({
            money: money
        });
    },
    bindPickerChange(e) {
        this.setData({
            index: e.detail.value
        });
    },
    getBlanceSet() {
        let that = this;
        common.request({
            endpoint: "Balance/balance/balance-set-read",
            method: "post",
            data: {},
            success: res => {
                if (res.code == 0) {
                    if (res.extra.switch != 1) {
                        that.setData({
                            flag: false
                        });
                    }
                    that.setData({
                        array: res.extra.bank_names
                    });
                    console.log(that.data.array);
                }
            }
        });
    },
    applyWithdrawal() {
        if (!this.data.money) {
            wx.showModal({
                title: "提示",
                content: "请填写提现金额"
            });
            return;
        }
        if (!this.data.array[this.data.index]) {
            wx.showModal({
                title: "提示",
                content: "请选择银行卡"
            });
            return;
        }
        if (!this.data.cardName) {
            wx.showModal({
                title: "提示",
                content: "请填写持卡人姓名"
            });
            return;
        }
        if (!this.data.blankCard || this.data.blankCard.length < 19) {
            wx.showModal({
                title: "提示",
                content: "请填写正确的银行卡号"
            });
            return;
        }
        let that = this;
        common.request({
            endpoint: "Balance/balance/cash-out",
            method: "post",
            data: {
                price: that.data.money,
                bank_name: that.data.array[that.data.index],
                account: that.data.blankCard,
                name: that.data.cardName
            },
            success: res => {
                that.setData({
                    page: 1,
                    list: []
                });
                that.getBase();
                that.getRecordList();
                that.getBlanceSet();
                if (res.code == 0) {
                    wx.showModal({
                        title: "提示",
                        content: "提交成功",
                        complete: res => {}
                    });
                }
            },
            fail: res => {
                wx.showModal({
                    title: "提示",
                    content: res.message
                });
            }
        });
    },
    getRecordList() {
        let that = this;
        common.request({
            endpoint: "Balance/balance/cash-out-bill",
            method: "post",
            data: {
                page: that.data.page,
                rows: 10
            },
            success: res => {
                if (that.data.list.length < 1) {
                    that.setData({
                        list: res.extra,
                        page: that.data.page + 1
                    });
                } else {
                    that.setData({
                        list: that.data.list.concat(res.extra),
                        page: that.data.page + 1
                    });
                }
            },
            fail: res => {}
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        this.getRecordList();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //获取基础配置
    //全局基础配置信息
    getBase() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: "提现"
                });
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    leader_express: res.extra.l_express,
                    rp_name: res.extra.rp_name,
                    order_start: res.extra.order_start,
                    order_start_switch: res.extra.order_start_switch,
                    m_apply: res.extra.m_apply
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
            }
        });
    }
});